{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "# python 3.7\n",
    "import pandas as pd\n",
    "import math\n",
    "from math import log\n",
    "\n",
    "#导入数组\n",
    "df = pd.read_csv('1.csv') #DataFrame\n",
    "\n",
    "#清洗数据，删除无关列\n",
    "dfclean = df.drop(columns = ['y-coordinate','x_velocity','y_velocity','z_velocity'])\n",
    "\n",
    "#提取一列数据\n",
    "#df[\"y\"].unique()\n",
    "#去除重复\n",
    "#df.duplicated()\n",
    "#df.drop_duplicates()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "#删除列\n",
    "df_z = dfclean.drop(columns = ['x','y'])\n",
    "\n",
    "#去除重复\n",
    "array_1 = df_z.drop_duplicates()\n",
    "\n",
    "#将列z转换为数组\n",
    "arrayz = array_1[\"z\"].unique()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "#求数组长度\n",
    "l = len (arrayz)\n",
    "\n",
    "#第一层网格中心点高度\n",
    "h0 = 0.0\n",
    "\n",
    "#创建空数组\n",
    "ea1 = []\n",
    "ea2 = []\n",
    "\n",
    "for i in range(0, l) :\n",
    "    \n",
    "    arrayz[i]\n",
    "    \n",
    "    #创建交换空间储存对应z值的行\n",
    "    swap = dfclean.loc[dfclean['z'] == arrayz[i]]\n",
    "    \n",
    "    #提取交换空间中y列到数列\n",
    "    arrayh = swap[\"y\"].unique()\n",
    "\n",
    "    #求数组长度\n",
    "    b = len(arrayh)\n",
    "        \n",
    "        \n",
    "    for a in range(0, b) :\n",
    "        \n",
    "        #求高度起始值\n",
    "        h_min = min (arrayh)\n",
    "\n",
    "        arrayh[a]\n",
    "        \n",
    "        #求相对高度\n",
    "        h_ref = abs(arrayh[a]-h_min)+ h0\n",
    "        \n",
    "        #将求解结果写入数组\n",
    "        ea1.append(h_ref)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "k = 0.41\n",
    "ustar = 2.0\n",
    "z0 = 0.5\n",
    "\n",
    "for c in  range(0, len (ea1)) :\n",
    "    v = log(ea1[c]/z0+1)*ustar/k\n",
    "    ea2.append(v)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "ex3 = pd.DataFrame(ea2)\n",
    "ex3 = ex3.set_axis(['velocity'], axis='columns', inplace=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>x</th>\n",
       "      <th>y</th>\n",
       "      <th>z</th>\n",
       "      <th>velocity</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>337.13849</td>\n",
       "      <td>-4865.0801</td>\n",
       "      <td>-3537</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>337.13849</td>\n",
       "      <td>-4864.0552</td>\n",
       "      <td>-3537</td>\n",
       "      <td>5.439395</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>337.13849</td>\n",
       "      <td>-4862.9795</td>\n",
       "      <td>-3537</td>\n",
       "      <td>8.043363</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>337.13849</td>\n",
       "      <td>-4861.8501</td>\n",
       "      <td>-3537</td>\n",
       "      <td>9.802709</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>337.13849</td>\n",
       "      <td>-4860.6641</td>\n",
       "      <td>-3537</td>\n",
       "      <td>11.149475</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>43843</th>\n",
       "      <td>337.13849</td>\n",
       "      <td>-4463.0454</td>\n",
       "      <td>7743</td>\n",
       "      <td>21.554685</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>43844</th>\n",
       "      <td>337.13849</td>\n",
       "      <td>-4440.9946</td>\n",
       "      <td>7743</td>\n",
       "      <td>23.633667</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>43845</th>\n",
       "      <td>337.13849</td>\n",
       "      <td>-4417.9614</td>\n",
       "      <td>7743</td>\n",
       "      <td>25.142438</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>43846</th>\n",
       "      <td>337.13849</td>\n",
       "      <td>-4394.4429</td>\n",
       "      <td>7743</td>\n",
       "      <td>26.314682</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>43847</th>\n",
       "      <td>337.13849</td>\n",
       "      <td>-4370.8682</td>\n",
       "      <td>7743</td>\n",
       "      <td>27.261145</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>43848 rows × 4 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "               x          y     z   velocity\n",
       "0      337.13849 -4865.0801 -3537   0.000000\n",
       "1      337.13849 -4864.0552 -3537   5.439395\n",
       "2      337.13849 -4862.9795 -3537   8.043363\n",
       "3      337.13849 -4861.8501 -3537   9.802709\n",
       "4      337.13849 -4860.6641 -3537  11.149475\n",
       "...          ...        ...   ...        ...\n",
       "43843  337.13849 -4463.0454  7743  21.554685\n",
       "43844  337.13849 -4440.9946  7743  23.633667\n",
       "43845  337.13849 -4417.9614  7743  25.142438\n",
       "43846  337.13849 -4394.4429  7743  26.314682\n",
       "43847  337.13849 -4370.8682  7743  27.261145\n",
       "\n",
       "[43848 rows x 4 columns]"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#添加新列并赋值\n",
    "#dfclean['velocity']='ea1'\n",
    "\n",
    "#dffinal = dfclean.merge(ex3, left_on='x', right_on='velocity', suffixes=('_left', '_right'))\n",
    "\n",
    "#合并dataframe\n",
    "dffinal = dfclean.join(ex3)\n",
    "dffinal"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "#将DataFrame写入csv\n",
    "dffinal.to_csv('velocit.csv')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
